package com.huonan.jb.tools.fileuse.compare;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class Cache {
    private static final Cache instance = new Cache();
    private final ConcurrentMap<Object, Object> cache = new ConcurrentHashMap<>();

    private Cache() {
        // 私有构造函数，防止类外部实例化
    }

    public static Cache getInstance() {
        return instance;
    }

    public void put(Object key, Object value) {
        cache.put(key, value);
    }

    public Map<String, String> getMap(String key) {
        return (Map<String, String>) cache.get(key);
    }

    public Object getStr(String key) {
        return cache.get(key);
    }

    public boolean containKey(Object key) {
        return cache.containsKey(key);
    }

    public Set<Map.Entry<String, String>> getMapAll(String key) {
        Map<String, String> o = getMap(key);
        return o.entrySet();
    }

    public void remove(Object key) {
        cache.remove(key);
    }

    public void clear() {
        cache.clear();
    }

    public boolean containMapKey(String key, String mapKey) {
        Map<String, String> o = getMap(key);
        if (o == null) {
            return false;
        }
        return o.containsKey(mapKey);
    }

    public String getMap(String key, String mapKey) {
        Map<String, String> o = getMap(key);
        return o != null ? o.get(mapKey) : null;
    }

    public void putMap(String key, String mapKey, String value) {
        Map<String, String> o = getMap(key);
        if (o == null) {
            o = new HashMap<>();
        }
        o.put(mapKey, value);
        cache.put(key, o);
    }

    public void putMapValueAdd(String key, String mapKey) {
        Cache cache = Cache.getInstance();
        if (!cache.containMapKey(key, mapKey)) {
            cache.putMap(key, mapKey, String.valueOf(1));
        } else {
            Map<String, String> map = cache.getMap(key);
            int mapValue = Integer.parseInt(map.get(mapKey));
            mapValue++;
            cache.putMap(key, mapKey, String.valueOf(mapValue));
        }
    }

    public void putMapValueContact(String key, String mapKey, String filePath) {
        Cache cache = Cache.getInstance();
        if (!cache.containMapKey(key, mapKey)) {
            cache.putMap(key, mapKey, filePath);
        } else {
            Map<String, String> map = cache.getMap(key);
            String mapValue = map.get(mapKey);
            mapValue = mapValue + "-" + filePath;
            cache.putMap(key, mapKey, mapValue);
        }
    }

    public void putList(String key, String value) {
        Cache cache = Cache.getInstance();
        if (cache.containKey(key)) {
            List<String> list = cache.getList(key);
            list.add(value);
            cache.put(key, list);
        } else {
            List<String> list = new ArrayList<>();
            list.add(value);
            cache.put(key, list);
        }
    }

    private List<String> getList(String key) {
        return (List<String>) cache.get(key);
    }
}